package input;

import static org.lwjgl.input.Keyboard.KEY_A;
import static org.lwjgl.input.Keyboard.KEY_BACK;
import static org.lwjgl.input.Keyboard.KEY_COMMA;
import static org.lwjgl.input.Keyboard.KEY_DOWN;
import static org.lwjgl.input.Keyboard.KEY_ESCAPE;
import static org.lwjgl.input.Keyboard.KEY_I;
import static org.lwjgl.input.Keyboard.KEY_J;
import static org.lwjgl.input.Keyboard.KEY_K;
import static org.lwjgl.input.Keyboard.KEY_L;
import static org.lwjgl.input.Keyboard.KEY_LEFT;
import static org.lwjgl.input.Keyboard.KEY_NUMPAD0;
import static org.lwjgl.input.Keyboard.KEY_NUMPAD2;
import static org.lwjgl.input.Keyboard.KEY_NUMPAD4;
import static org.lwjgl.input.Keyboard.KEY_NUMPAD6;
import static org.lwjgl.input.Keyboard.KEY_NUMPAD8;
import static org.lwjgl.input.Keyboard.KEY_RETURN;
import static org.lwjgl.input.Keyboard.KEY_RIGHT;
import static org.lwjgl.input.Keyboard.KEY_S;
import static org.lwjgl.input.Keyboard.KEY_SPACE;
import static org.lwjgl.input.Keyboard.KEY_UP;
import static org.lwjgl.input.Keyboard.KEY_X;
import static org.lwjgl.input.Keyboard.KEY_Z;
import static org.lwjgl.input.Keyboard.getEventKey;
import static org.lwjgl.input.Keyboard.getEventKeyState;
import static org.lwjgl.input.Keyboard.next;

import java.util.HashMap;

import org.lwjgl.LWJGLException;

/**
 * Classe que supre os eventos do teclado.
 * Nen publica nen privada, vista apenas no pacote.
 * @author Ygor
 *
 */
class KeyboardListener implements IInputListener{
	
	HashMap<Integer, Key> mappings;
	
	KeyboardListener() {
		mappings = new HashMap<Integer, Key>();
		
		mappings.put(KEY_A, InputManager.keyboard.confirm);
		mappings.put(KEY_Z, InputManager.keyboard.confirm);
		mappings.put(KEY_SPACE, InputManager.keyboard.confirm);
		mappings.put(KEY_RETURN, InputManager.keyboard.confirm);
		mappings.put(KEY_NUMPAD0, InputManager.keyboard.confirm);
		
		mappings.put(KEY_S, InputManager.keyboard.cancel);
		mappings.put(KEY_X, InputManager.keyboard.cancel);
		mappings.put(KEY_BACK, InputManager.keyboard.cancel);
		mappings.put(KEY_COMMA, InputManager.keyboard.cancel); // do lado do zero
		
		mappings.put(KEY_UP, InputManager.keyboard.up);
		mappings.put(KEY_NUMPAD8, InputManager.keyboard.up);
		mappings.put(KEY_I, InputManager.keyboard.up);

		mappings.put(KEY_DOWN, InputManager.keyboard.down);
		mappings.put(KEY_NUMPAD2, InputManager.keyboard.down);
		mappings.put(KEY_K, InputManager.keyboard.down);
		
		mappings.put(KEY_LEFT, InputManager.keyboard.left);
		mappings.put(KEY_NUMPAD4, InputManager.keyboard.left);
		mappings.put(KEY_J, InputManager.keyboard.left);
		
		mappings.put(KEY_RIGHT, InputManager.keyboard.right);
		mappings.put(KEY_NUMPAD6, InputManager.keyboard.right);
		mappings.put(KEY_L, InputManager.keyboard.right);
		
		mappings.put(KEY_ESCAPE, InputManager.keyboard.exit);
	}
	
	@Override
	public void pool() {
	    org.lwjgl.input.Keyboard.poll();
		
		while (next()) {
			Key target = mappings.get(getEventKey());
			if (null != target) {
				target.toogle(getEventKeyState());
			}
		}
	}

	@Override
	public void create() {
		try {
			org.lwjgl.input.Keyboard.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() {
		org.lwjgl.input.Keyboard.destroy();
	}
}
